<?php

namespace app\model;

use think\Model;

/**
 * 消息表
 *
 * @author yyzh
 *        
 */
class SysMessage extends Model {
	protected $table = 'sys_message';
	protected $pk = 'id';
	protected $field = array (
			'id',
			'account',
			'title',
			'content',
			'type',
			'state',
			'param',
			'link',
			'send',
			'send_time' 
	);
	
	/**
	 * 统计未读消息条数
	 *
	 * @param unknown $account
	 * @return number
	 */
	public static function countUnread($account) {
		return self::where ( [ 
				'account' => $account,
				'state' => 0 
		] )->count ( 0 );
	}
	/**
	 * 获取未读消息
	 *
	 * @param unknown $account 账号
	 * @return \think\static[]|\think\false
	 */
	public static function unread($account) {
		return self::all ( function ($query) use ($account) {
			$query->where ( 'account', $account );
			$query->where ( "state", 0 );
			$query->limit ( 5 );
		}, [ ], true );
	}
	public function getTypeTextAttr($value, $data) {
		$tmp = [ 
				0 => '普通消息',
				1 => '重要消息',
				2 => '通知消息',
				3 => '定时消息' 
		];
		return $tmp [$data ['type']];
	}
	public function getStateTextAttr($value, $data) {
		$tmp = [ 
				0 => '未读',
				1 => '已读' 
		];
		return $tmp [$data ['state']];
	}
}